package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryTreeItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
@Api(tags = "07. 分类模块")
@Validated
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("add-new")
    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理添加分类的请求，参数为: {}", categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);
        log.debug("处理添加分类的请求结束，完成");

        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(1);
        jsonResult.setMessage("添加成功");
        return jsonResult;
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "long"),

    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除分类失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除分类】的请求，参数：{}", id);
        categoryService.delete(id);
        log.debug("处理【根据id删除分类】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改分类")
    @ApiOperationSupport(order = 300)
    public JsonResult updateById(@Range(min = 1, message = "根据ID更新分类失败，请提交合法的ID值！")
                                 @RequestParam Long categoryId,
                                 @Valid CategoryUpdateInfoParam categoryUpdateInfoParam) {
        log.debug("开始处理【修改分类】的请求，参数：{}", categoryId, categoryUpdateInfoParam);
        categoryService.updateById(categoryId, categoryUpdateInfoParam);
        log.debug("处理【修改分类】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/setEnable")
    @ApiOperation("启用")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@Range(min = 1, message = "id错误！") @RequestParam Long id) {
        log.debug("开始处理【启用】的请求，参数：{}", id);
        categoryService.Enable(id);
        log.debug("处理【启用】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/setDisabled")
    @ApiOperation("禁用")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long")
    })
    public JsonResult setDisabled(@Range(min = 1, message = "id错误！") @RequestParam Long id) {
        log.debug("开始处理【禁用】的请求，参数：{}", id);
        categoryService.Disable(id);
        log.debug("处理【禁用】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/listCategory")
    @ApiOperation("根据父级id查询列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父级id", required = true, dataType = "long")
    })
    public JsonResult listCategory(@Range(min = 0, message = "父级id最小为零") @RequestParam Long id) {
        log.debug("开始处理【根据父级id查询列表】的请求，参数：{}", id);
        List<CategoryListItemVO> categoryListItemByParentIdVOList = categoryService.listCategory(id);
        log.debug("处理【根据父级id查询列表】的请求，完成！");
        return JsonResult.ok(categoryListItemByParentIdVOList);
    }

    @GetMapping("/getStandard")
    @ApiOperation("根据id查询数据")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long")
    })
    public JsonResult getStandard(@Range(min = 1, message = "id最小为零") @RequestParam Long id) {
        log.debug("开始处理【根据id查询数据】的请求，参数：{}", id);
        CategoryStandardVO categoryStandardVO = categoryService.getStandard(id);
        log.debug("处理【根据id查询数据】的请求，完成！");
        return JsonResult.ok(categoryStandardVO);
    }

    @GetMapping("/listTree")
    @ApiOperation("查询树状列表")
    @ApiOperationSupport(order = 430)
    public JsonResult listTree() {
        log.debug("开始处理【查询树状列表】的请求");
        List<CategoryTreeItemVO> categoryTreeItemVOList = categoryService.listTree();
        log.debug("处理【查询树状列表】的请求完成！");
        return JsonResult.ok(categoryTreeItemVOList);
    }
}
